//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_GDBSTUB_H__
#define __ELASTOS_GDBSTUB_H__

/* The gdb remote communication protocol.

   A debug packet whose contents are <data>
   is encapsulated for transmission in the form:

        $ <data> # CSUM1 CSUM2

        <data> must be ASCII alphanumeric and cannot include characters
        '$' or '#'.  If <data> starts with two characters followed by
        ':', then the existing stubs interpret this as a sequence number.

        CSUM1 and CSUM2 are ascii hex representation of an 8-bit
        checksum of <data>, the most significant nibble is sent first.
        the hex digits 0-9,a-f are used.

   Receiver responds with:

        +       - if CSUM is correct and ready for next packet
        -       - if CSUM is incorrect

   <data> is as follows:
   All values are encoded in ascii hex digits.

        Request         Packet

        read registers  g
        reply           XX....X         Each byte of register data
                                        is described by two hex digits.
                                        Registers are in the internal order
                                        for GDB, and the bytes in a register
                                        are in the same order the machine uses.
                        or ENN          for an error.

        write regs      GXX..XX         Each byte of register data
                                        is described by two hex digits.
        reply           OK              for success
                        ENN             for an error

        write reg       Pn...=r...      Write register n... with value r...,
                                        which contains two hex digits for each
                                        byte in the register (target byte
                                        order).
        reply           OK              for success
                        ENN             for an error
        (not supported by all stubs).

        read mem        mAA..AA,LLLL    AA..AA is address, LLLL is length.
        reply           XX..XX          XX..XX is mem contents
                                        Can be fewer bytes than requested
                                        if able to read only part of the data.
                        or ENN          NN is errno

        write mem       MAA..AA,LLLL:XX..XX
                                        AA..AA is address,
                                        LLLL is number of bytes,
                                        XX..XX is data
        reply           OK              for success
                        ENN             for an error (this includes the case
                                        where only part of the data was
                                        written).

        write mem       XAA..AA,LLLL:XX..XX
         (binary)                       AA..AA is address,
                                        LLLL is number of bytes,
                                        XX..XX is binary data
        reply           OK              for success
                        ENN             for an error

        cont            cAA..AA         AA..AA is address to resume
                                        If AA..AA is omitted,
                                        resume at same address.

        step            sAA..AA         AA..AA is address to resume
                                        If AA..AA is omitted,
                                        resume at same address.

        last signal     ?               Reply the current reason for stopping.
                                        This is the same reply as is generated
                                        for step or cont : SAA where AA is the
                                        signal number.

        There is no immediate reply to step or cont.
        The reply comes when the machine stops.
        It is           SAA             AA is the "signal number"

        or...           TAAn...:r...;n:r...;n...:r...;
                                        AA = signal number
                                        n... = register number
                                        r... = register contents
        or...           WAA             The process exited, and AA is
                                        the exit status.  This is only
                                        applicable for certains sorts of
                                        targets.
        kill request    k

        toggle debug    d               toggle debug flag (see 386 & 68k stubs)
        reset           r               reset -- see sparc stub.
        reserved        <other>         On other requests, the stub should
                                        ignore the request and send an empty
                                        response ($#<checksum>).  This way
                                        we can extend the protocol and GDB
                                        can tell whether the stub it is
                                        talking to uses the old or the new.
        search          tAA:PP,MM       Search backwards starting at address
                                        AA for a match with pattern PP and
                                        mask MM.  PP and MM are 4 bytes.
                                        Not supported by all stubs.

        general query   qXXXX           Request info about XXXX.
        general set     QXXXX=yyyy      Set value of XXXX to yyyy.
        query sect offs qOffsets        Get section offsets.  Reply is
                                        Text=xxx;Data=yyy;Bss=zzz
        console output  Otext           Send text to stdout.  Only comes from
                                        remote target.

        Responses can be run-length encoded to save space.  A '*' means that
        the next character is an ASCII encoding giving a repeat count which
        stands for that many repititions of the character preceding the '*'.
        The encoding is n+29, yielding a printable character where n >=3
        (which is where rle starts to win).  Don't use an n > 126.

        So
        "0* " means the same as "0000".
*/
#include <core.h>
#include <_hal.h>
#include <eladef.h>

#if (defined(_UDBAGENT) || defined(_KDBAGENT))

#ifndef MODULEINFO
#define MODULEINFO 1000
#endif

const char c_arrHexChars[] = "0123456789abcdef";
const size_t c_uBufSize = 1000;

#if defined(_KDBAGENT)
const int_t c_nNumberOfBreakpoints = 500;
#endif

#define BREAK_INSTRUCTION ELASTOS_HARD_BREAKPOINT
#define BREAK_INSSTRING ELASTOS_HARD_BPSTRING

#ifdef _x86
#define GDB_FLUSH_CACHE
#endif

#ifdef _arm
#define GDB_FLUSH_CACHE FlushCache();

extern StubContext *g_cpArmCurrentCxt;
extern UInt32 GetRegister(int reg);
UInt32* arm_target_ins(UInt32 *pc, UInt32 ins);
#endif

// only for software breakpoint
typedef struct BREAKPOINT : public DLinkNode {
      byte_t *pAddr;
      char buf[10]; // TODO: define SIZEOF_xxx
} Breakpoint;

EXTERN_C void StubInnerInit();

EXTERN_C void SetMemError(void);

EXTERN_C bool_t HexCharToInt(const char ch, int_t *pn);

EXTERN_C bool_t HexStrToByte(const char *pszBuf, int_t *pn);

EXTERN_C const char* HexWordStrToInt(const char *pszBuf, int_t *pVal);

EXTERN_C const char* ParseMemCommand(const char *pszBuf,
                                    byte_t **ppAddr, int_t *pCount);

EXTERN_C bool_t HexStrToMem(const char *pszBuf, byte_t *pMem, int_t nCount);

EXTERN_C const char *ParseRegCommand(const char *pszBuf, int_t* pReg,
                                        byte_t *pRegbytes, int_t *pCount);

EXTERN_C char* MemToHexStr(const byte_t *pMem, char *pszBuf, int_t nCount);

EXTERN_C bool_t ParseBreakpointCommand(const char *pszBuf, int_t *pType,
                                    byte_t **ppAddr, int_t *pLen);

EXTERN_C void GetTargetCurrentState(const vector_t vector,
                                    StubContext *pStubCxt);

EXTERN_C int_t ExceptionVectorToSignals (const int_t nExceptionVector);

EXTERN_C void DebugPrint(char *pidentify, char *pszInfo);
#endif //#if (defined(_UDBAGENT) || defined(_KDBAGENT))

#endif //__ELASTOS_GDBSTUB_H__
